<template>
	<view class="leave-container">
		<view class="leave-list">
			<view class="leave-item" v-for="(item, index) in leaveList" :key="item.id">
				<view class="leave-item-l"  @tap="showImg(index)">
					<view class="leave-img">
						<image :src="item.user.avatar" mode="aspectFill"></image>
					</view>
					<view class="leave-detail">
						<view class="">{{item.user.nickname}}提交的请假申请</view>
						<view class="con-text">请假类型：{{item.type_name}}</view>
						<view class="con-text">请假原因：{{item.reason}}</view>
						<view v-if="item.reject_reason" class="con-text">拒绝原因：{{item.reject_reason}}</view>
						<view class="con-text">请假时间：{{item.leave_time}}</view>
						<view :class="'status leave-'+item.status">{{item.status_name}}</view>
					</view>
				</view>
				<view class="leave-item-r" v-if="item.status === 0">
					<view class="success" @tap="throughLeave(index, item)">通过</view>
					<view class="error" @tap="showAlert(index, item)">驳回</view>
				</view>
			</view>
			<uni-load-more :status="loadStatus"></uni-load-more>
		</view>
		<openAlert ref="openAlertReason" :AlertClass="0" :AlertPosition="'center'" :BgColor="'rgba(50,50,50,0.8)'">
			<view class="share-box">
				<view class="open-con">
					<view class="title">驳回原因</view>
					<view class="open-content">
						<text>驳回原因：</text>
						<input type="text" value="" placeholder="请输入驳回原因" v-model="formData.reject_reason" />
					</view>
					<view class="btns">
						<view class="cancel" @tap="cancel">取消</view>
						<view class="confirm" @tap="rejectLeave">确认</view>
					</view>
				</view>
			</view>
		</openAlert>
	</view>
</template>

<script>
	import {
		appLogin,
		msg,
		httpGet,
		httpPost
	} from '@/utils/common.js';
	import openAlert from '@/components/open-alert/open-alert.vue';
	export default {
		components: {
			openAlert
		},
		data() {
			return {
				userInfo: {},
				leaveList: [],
				page: 1,
				page_size: 10,
				loadStatus: 'more',
				formData: {}
			}
		},
		onLoad(options) {
			appLogin(res => {
				this.userInfo = res;
				this.getLeaveList();
			})
		},
		onReachBottom() {
			if (this.loadStatus === 'nomre') {
				return
			}
			this.page++;
			this.getLeaveList();
		},
		methods: {
			getLeaveList() {
				httpGet({
					url: 'users/leave/list',
					data: {
						page: this.page,
						// page_size: this.page_size
					}
				}).then(res => {
					let {
						count,
						list
					} = res.result;

					this.loadStatus = 'more';
					let totalPage = Math.ceil(count / this.page_size);
					if (this.page >= totalPage) {
						this.loadStatus = 'nomore';
					}
					if (!list || !list.length) {
						this.loadStatus = 'nomore';
					}
					if (this.page == 1) {
						this.leaveList = list;
					} else {
						this.leaveList = [...this.leaveList, ...list];
					}
				})
			},

			throughLeave(index, leave_id) {
				uni.showModal({
					title: '提示',
					content: '确认通过审核？',
					success: (res) => {
						if (res.confirm) {
							let obj = {
								leave_id: this.leaveList[index].leave_id,
								status: 1
							}
							this.verifyPost(obj);
						}
					}
				})
			},

			showAlert(index, item) {
				this.$refs.openAlertReason.Show();
				this.formData = {
					leave_id: this.leaveList[index].leave_id,
					status: -1,
				}
			},
			
			cancel() {
				this.$refs.openAlertReason.Close();
			},

			rejectLeave() {
				let params = {
					...this.formData
				}
				if (!params.reject_reason) {
					msg('请输入拒绝理由！')
					return;
				}
				this.verifyPost(this.formData);
			},

			verifyPost(params) {
				httpPost({
					url: 'users/leave/verify',
					data: params
				}).then(res => {
					msg('操作成功！', 'none', () => {
						this.page = 1;
						this.leaveList = [];
						this.getLeaveList();
						this.cancel();
					});
				})
			},
			
			showImg(index){
				var imgs = this.leaveList[index].leave_imgs
				if(!imgs || !imgs.length) return
				// 预览图片
				uni.previewImage({
					urls: imgs,
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #f6f6f6;
	}

	.leave-container {
		.leave-list {

			margin-top: 20rpx;

			.leave-item {
				background-color: #fff;
				padding: 30rpx 20rpx;
				margin-top: 10rpx;
				display: flex;
				justify-content: space-between;

				.leave-item-l {
					display: flex;

					.leave-img {
						height: 100rpx;
						width: 100rpx;
						margin-right: 20rpx;

						image {
							height: 100rpx;
							width: 100rpx;
							border-radius: 50%;
						}
					}

					.leave-detail {
						.con-text {
							font-size: 26rpx;
							color: #999;
							margin: 5rpx;
						}

						.status {
							font-size: 26rpx;
						}

						.leave-0 {
							color: blue;
						}

						.leave-1 {
							color: green;
						}

						.leave-2 {
							color: red;
						}
					}
				}

				.leave-item-r {
					font-size: 26rpx;

					view {
						padding: 9rpx 24rpx;
						white-space: nowrap;
						color: #fff;
						font-size: 24rpx;
						border-radius: 50rpx;
					}

					.success {
						background-color: #3086ff;
					}

					.error {
						background-color: #fc6053;
						margin-top: 20rpx;
					}
				}
			}
		}

		.share-box {
			padding: 20rpx;
			.open-con {
				background-color: #fff;
				border-radius: 14rpx;
				// height: 420rpx;
				.title {
					padding: 30rpx 0;
					text-align: center;
					background-color: #fff;
					border-radius: 14rpx 14rpx 0 0;
				}

				.open-content {
					padding: 30rpx 20rpx 50rpx;
					display: flex;
					align-items: center;
					color: #333;
					width: 560rpx;
					// overflow: hidden;
				}
				.btns {
					display: flex;
					align-items: center;
					border-top: 1px solid #eee;
					view {
						width: 50%;
						height: 90rpx;
						line-height: 90rpx;
						text-align: center;
						font-weight: bold;
					}
					.confirm {
						color: #3086ff;
						border-left: 1px solid #eee;
					}
				}
			}
		}
	}
</style>
